package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Breakrulerecord;


import com.woniuxy.caradmin.entity.pojo.Yearcheckrecord;
import com.woniuxy.caradmin.entity.vo.BreakrulerecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.BreakrulerecordService;
import com.woniuxy.caradmin.util.Constaint;

import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController

public class BreakrulerecordController {
    @Autowired
    private BreakrulerecordService breakrulerecordService;


    @GetMapping("/breakrulerecordById")
    @ApiOperation(value = "id查询年检记录")
    public R yearcheckrecordById(Integer id) {

        Breakrulerecord breakrulerecord = breakrulerecordService.selectBreakById(id);
        if (breakrulerecord !=null) {
//            Date time  = yearcheckrecord.getTime();
//            Date nexttime  = yearcheckrecord.getNexttime();
//            java.sql.Date timesql=new java.sql.Date(time.getTime());
//            java.sql.Date nexttimesql=new java.sql.Date(nexttime.getTime());
//            yearcheckrecord.setTime(timesql);
//            yearcheckrecord.setNexttime(nexttimesql);
            return R.ok().data("breakrulerecord",breakrulerecord);
        } else {
            throw new CarException(ResultCode.BREAKRRISNULL, Constaint.BREAKRRISNULL);
        }
    }
    @GetMapping("/breakrulerecordList")
    @ApiOperation("违章记录")
    public DataGridView selectAll(BreakrulerecordVo breakrulerecordVo){
        Page<Object> page = PageHelper.startPage(breakrulerecordVo.getPage(), breakrulerecordVo.getLimit());
        List<Breakrulerecord> breakrulerecords = breakrulerecordService.selectAll(breakrulerecordVo);
        if(breakrulerecords!=null && breakrulerecords.size()>0){
            return  new DataGridView(page.getTotal(),breakrulerecords);
        }else {
            throw new CarException(ResultCode.BREAKRRISNULL , Constaint.BREAKRRISNULL);
        }
    }
    @GetMapping("/breakrulerecords")
    @ApiOperation("查询违章记录")
    public DataGridView selectAllby(BreakrulerecordVo breakrulerecordVo){
        Page<Object> page = PageHelper.startPage(breakrulerecordVo.getPage(), breakrulerecordVo.getLimit());
        List<Breakrulerecord> breakrulerecords = breakrulerecordService.selectAllBy(breakrulerecordVo);
        if(breakrulerecords!=null && breakrulerecords.size()>0){
            return  new DataGridView(page.getTotal(),breakrulerecords);
        }else {
            throw new CarException(ResultCode.BREAKRRISNULL , Constaint.BREAKRRISNULL);
        }
    }
    @GetMapping("/addbreakrulerecord")
    @ApiOperation("提交违章记录")
    public R addbreakrulerecord(Breakrulerecord breakrulerecord, HttpServletRequest httpServletRequest){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String vid = httpServletRequest.getParameter("vid");
        System.out.println(vid);
        String pid = httpServletRequest.getParameter("pid");
        String time = httpServletRequest.getParameter("time");
        String fine = httpServletRequest.getParameter("fine");
        String point = httpServletRequest.getParameter("point");
        String driverid = httpServletRequest.getParameter("driverid");
        String address = httpServletRequest.getParameter("address");
        String information = httpServletRequest.getParameter("information");
        breakrulerecord.setVid(Integer.parseInt(vid));
        breakrulerecord.setPid(Integer.parseInt(pid));
        breakrulerecord.setStatus(1);

        java.sql.Date d2=null;
        try {
            Date d1 = sdf.parse(time);
             d2= new java.sql.Date(d1.getTime());
        } catch (ParseException e) {
            breakrulerecord.setTime(d2);
        }

        breakrulerecord.setFine(Double.valueOf(fine));
        breakrulerecord.setPoint(Integer.parseInt(point));
        breakrulerecord.setAddress(address);
        breakrulerecord.setDriverid(Integer.parseInt(driverid));
        breakrulerecord.setInformation(information);
        int i= breakrulerecordService.addBreakrulerecord(breakrulerecord);
        if(i!=0){
            return R.ok().code(ResultCode.BREAKRRSUC).message(Constaint.BREAKRRSUC);
        }else {
            throw new CarException(ResultCode.BREAKRRFAIL , Constaint.BREAKRRFAIL);
        }
    }
    @GetMapping("/updatebreakrulerecord")
    @ApiOperation("更新违章记录")
    public  R updatebreakrulerecord(Breakrulerecord breakrulerecord){
        int i = breakrulerecordService.updateBreakrulerecord(breakrulerecord);
        if(i!=0){
            return  R.ok().code(ResultCode.UPBREAKRRSUC).message(Constaint.UPBREAKRRSUC);
        }else {
            throw  new CarException(ResultCode.UPBREAKRRFAIL,Constaint.UPBREAKRRFAIL);
        }
    }
    @GetMapping("/removeB")
    @ApiOperation("删除违章记录")
    public  R removeB(Integer[] ids){
        boolean b = breakrulerecordService.removeB(ids);
        if(b){
            return  R.ok().code(ResultCode.REMOVEBRRSUC).message(Constaint.REMOVEBRRSUC);
        }else {
            throw  new CarException(ResultCode.REMOVEBRRFAIL,Constaint.REMOVEBRRFAIL);
        }
    }


}
